<template>
  <div style="height:6vh;background-color: #fff;padding:10px 20px" >
    <span style="line-height:60px;font-size:20px">学校管理</span>
    <el-button @click="dialogVisible=true" type="primary" style="float:right;margin-top:13px;">新建学校</el-button>
  </div>
<!-- 新建学校弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose"
              style="width:1000px;padding:40px">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="学校名称">
            <el-input placeholder="请输入内容" v-model="saveSchoolForm.schoolName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学校城市">
            <el-input placeholder="请输入内容" v-model="saveSchoolForm.city"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveSchool">确定</el-button>
    </template>
  </el-dialog>
  <!-- 学校搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
        <el-form-item label="学校名称">
          <el-input placeholder="请输入内容" style="width: 200px;"
                    v-model="searchSchoolForm.schoolName" @keydown.enter="loadSchool"></el-input>
        </el-form-item>
      <el-form-item label="学校城市">
        <el-input placeholder="请输入内容" style="width: 200px;"
                  v-model="searchSchoolForm.city" @keydown.enter="loadSchool"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadSchool">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 学校列表 -->
  <el-card style="margin:20px;">
    <el-table :data="schoolArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="schoolName" label="学校名称" align="center"/>
      <el-table-column prop="city" label="学校城市" align="center"/>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button type="primary" size="mini" @click="editSchool(scope.row.id)">编辑</el-button>
            <el-button type="danger" size="mini" @click="deleteSchool(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
// 控制新建学校弹窗是否显示
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

const dialogVisible = ref(false)
// 定义变量控制dialog的标题
const dialogTitle = ref('新建学校')

// 定义数组保存学校列表信息
const schoolArr = ref([]);
// 定义对象保存查询条件 姓名 城市
const searchSchoolForm = ref({
  schoolName:'',
  city:''
});

//定义加载学校的方法
const loadSchool = () => {
  let data =qs.stringify(searchSchoolForm.value);
  console.log(data);
  axios.get(BASE_URL+'/v1/school/select?'+data).then((response)=>{
    if(response.data.code==2000){
      schoolArr.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

onMounted(()=>{
  loadSchool();// 一进入页面立即加载所有的学校信息
})

//准备对象用来收集新增/修改学校的表单信息
const saveSchoolForm=ref({
  schoolName:'',
  city:''
});

//保存学校信息方法
const saveSchool = () => {
  let data =qs.stringify(saveSchoolForm.value);
  console.log(data);
  axios.post(BASE_URL+'/v1/school/save/',data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success('保存成功');
      saveSchoolForm.value = {};
      dialogVisible.value = false;
      loadSchool();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

//关闭弹窗
const handleClose = () => {
  if (confirm('您确定要关闭本弹窗吗?')){
  dialogVisible.value = false;
  saveSchoolForm.value = {};
  }
}

const resetSearch = () => {
  searchSchoolForm.value = {};
  loadSchool();
}

//编辑学校
const editSchool = (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑学校';
  axios.get(BASE_URL+'/v1/school/select?id='+id).then((response)=>{
    if(response.data.code==2000){
      saveSchoolForm.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

//删除学校
const deleteSchool = (id) => {
  if (confirm('您确定要删除该学校吗?')){
    axios.post(BASE_URL+'/v1/school/delete/'+id).then((response)=>{
      if(response.data.code==2000){
        ElMessage.success('删除成功');
        loadSchool();
      }else{
        ElMessage.error(response.data.msg);
      }
    })
  }
}

</script>


<style scoped>

</style>